<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2012 Grzegorz Bańka <grzegorz@grzegorzbanka.com>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * [CLASS/FUNCTION INDEX of SCRIPT]
 *
 * Hint: use extdeveval to insert/update function index above.
 */


$LANG->includeLLFile('EXT:mt_importernews/mod1/locallang.xml');
require_once(PATH_t3lib . 'class.t3lib_scbase.php');
$BE_USER->modAccess($MCONF,1);	// This checks permissions and exits if the users has no permission for entry.
	// DEFAULT initialization of a module [END]



/**
 * Module 'Import NEWS' for the 'mt_importernews' extension.
 *
 * @author	Grzegorz Bańka <grzegorz@grzegorzbanka.com>
 * @package	TYPO3
 * @subpackage	tx_mtimporternews
 */
class  tx_mtimporternews_module1 extends t3lib_SCbase {
				var $pageinfo;

				/**
				 * Initializes the Module
				 * @return	void
				 */
				function init()	{
					global $BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;

					parent::init();

					/*
					if (t3lib_div::_GP('clear_all_cache'))	{
						$this->include_once[] = PATH_t3lib.'class.t3lib_tcemain.php';
					}
					*/
				}

				/**
				 * Adds items to the ->MOD_MENU array. Used for the function menu selector.
				 *
				 * @return	void
				 */
				function menuConfig()	{
					global $LANG;
					$this->MOD_MENU = Array (
						'function' => Array (
							'1' => $LANG->getLL('function1'),
							'2' => $LANG->getLL('function2'),
							'3' => $LANG->getLL('function3'),
						)
					);
					parent::menuConfig();
				}

				/**
				 * Main function of the module. Write the content to $this->content
				 * If you chose "web" as main module, you will need to consider the $this->id parameter which will contain the uid-number of the page clicked in the page tree
				 *
				 * @return	[type]		...
				 */
				function main()	{
					global $BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;

					// Access check!
					// The page will show only if there is a valid page and if this page may be viewed by the user
					$this->pageinfo = t3lib_BEfunc::readPageAccess($this->id,$this->perms_clause);
					$access = is_array($this->pageinfo) ? 1 : 0;
				
					if (($this->id && $access) || ($BE_USER->user['admin'] && !$this->id))	{

							// Draw the header.
						$this->doc = t3lib_div::makeInstance('mediumDoc');

						$this->doc->inDocStyles .= '
div.typo3-mediumDoc {
width:100% !important;
}
fieldset {
padding:5px;
margin:5px;
}
.mtis_rot {
padding:10px;
}
						';
						
						$this->doc->backPath = $BACK_PATH;
						$this->doc->form='<form action="" method="post" enctype="multipart/form-data">';

							// JavaScript
						$this->doc->JScode = '
							<script language="javascript" type="text/javascript">
								script_ended = 0;
								function jumpToUrl(URL)	{
									document.location = URL;
								}
							</script>
						';
						$this->doc->postCode='
							<script language="javascript" type="text/javascript">
								script_ended = 1;
								if (top.fsMod) top.fsMod.recentIds["web"] = 0;
							</script>
						';

						$headerSection = $this->doc->getHeader('pages', $this->pageinfo, $this->pageinfo['_thePath']) . '<br />'
							. $LANG->sL('LLL:EXT:lang/locallang_core.xml:labels.path') . ': ' . t3lib_div::fixed_lgd_cs($this->pageinfo['_thePath'], -50);

						$this->content.=$this->doc->startPage($LANG->getLL('title'));
						$this->content.=$this->doc->header($LANG->getLL('title'));
						$this->content.=$this->doc->spacer(5);
						$this->content.=$this->doc->section('',$this->doc->funcMenu($headerSection,t3lib_BEfunc::getFuncMenu($this->id,'SET[function]',$this->MOD_SETTINGS['function'],$this->MOD_MENU['function'])));
						$this->content.=$this->doc->divider(5);


						// Render content:
						$this->moduleContent();


						// ShortCut
						if ($BE_USER->mayMakeShortcut())	{
							$this->content.=$this->doc->spacer(20).$this->doc->section('',$this->doc->makeShortcutIcon('id',implode(',',array_keys($this->MOD_MENU)),$this->MCONF['name']));
						}

						$this->content.=$this->doc->spacer(10);
					} else {
							// If no access or if ID == zero

						$this->doc = t3lib_div::makeInstance('mediumDoc');
						

						
						$this->doc->backPath = $BACK_PATH;

						$this->content.=$this->doc->startPage($LANG->getLL('title'));
						$this->content.=$this->doc->header($LANG->getLL('title'));
						$this->content.=$this->doc->spacer(5);
						$this->content.=$this->doc->spacer(10);
					}
				
				}

				/**
				 * Prints out the module HTML
				 *
				 * @return	void
				 */
				function printContent()	{

					$this->content.=$this->doc->endPage();
					echo $this->content;
				}

				/**
				 * Generates the module content
				 *
				 * @return	void
				 */
				 function url_remove($remove,$www)
				 {
				 	$remove=array_values($remove);
				 	for($i=0; $i<count($remove); $i++)
					{
						if(stristr($www,$remove[$i])) return false;
					}
					return true;
				 }
				 function add_posts()
				 {
				 	for($i=1; $i<intval($_POST['mt_count_post']); $i++)
					{
						
						if(intval($_POST['mt_addtext'.$i])==1)
						{
							$mt_header=$this->add_post_header($_POST['mt_header'][$i],$_POST['mt_header_text'][$i]);
							$mt_category=$this->add_post_category($_POST['mt_category'][$i]);
							$mt_tag=$this->add_post_tag($_POST['mt_tag'][$i]);
							$mt_body=$this->add_post_body($_POST['mt_body'][$i],$_POST['mt_body_text'][$i]);
							
							$add_query=array(
								$mt_header,
								$mt_category,
								$mt_tag,
								$mt_body							
							);
							
							$insertArray=array(
							'pid' 			=>	intval($_GET['id']),
							'tstamp'		=>	time(),
							'crdate'		=>	time(),
							'datetime'		=>	time(),
							'title'			=>	$mt_header,
							'short'			=>	'',
							'bodytext'		=>	$mt_body,
							'author'		=>	$mt_category,
							'keywords'		=>	$mt_tag
							);					
							
							$GLOBALS['TYPO3_DB']->exec_INSERTquery(
								'tt_news',
								$insertArray    			
							);
							
						}
					}
					$url=$_POST['allurl'];
					
					$realurl=str_replace('http://',$url);
					list($realurl)=explode('/',$realurl);
					
					$number=1;
					if(stristr($url,'/page/'))
					{
						list($url,$number)=explode('/page/',$url);
						$number=str_replace('/','',$number);
					}
					$number++;
					
					$add= $this->get($url);
					preg_match_all('/href=[\"\']?([^\"\'>]*)[\"\']?[^>]*>(.*?)<\/a>/si',$add,$out,PREG_SET_ORDER);
					for($i=0; $i<count($out); $i++)
					{
						if(stristr($out[$i][1],'/page/'))
						{
							list($geturl)=explode('/page/',$out[$i][1]);
							$addurl=$geturl.'/page/'.$number.'/';
						}
					}
					
					$url=str_replace('//','/',$url.'/page/');
					
					
					
					return '
					<h1>Dodano ('.$i.') teści</h1>
					
					<label>Import url</label>
					<textarea name="allurl" cols="100" rows="5">'.$addurl.'</textarea>
					<input name="all_links" type="hidden" value="'.$_POST['all_links'].'" />	
					<input name="" type="submit" />	or <input name="mt_next" type="submit" value="Next" />
					';
				 }
				 function add_post_body($body,$text)
				 {
				 	for($i=0; $i<count($body); $i++)
					{
						if($body[$i]==1) $add[]=$text[$i];
					}
					return implode("\n",$add);
				 }
				 function add_post_tag($mt_tag)
				 {
				 	for($i=0; $i<count($mt_tag); $i++)
					{
						$add[]=$mt_tag[$i];
					}
					return implode(',',$add);
				 }
				 function add_post_category($mt_category)
				 {
				 	for($i=0; $i<count($mt_category); $i++)
					{
						$add[]=$mt_category[$i];
					}
					return implode(',',$add);
				 }
				function add_post_header($what,$header)
				{
					for($i=0; $i<count($what); $i++)
					{
						if($what[$i]==1) $add.=$header[$i];
					}
					return $add;
				}
				function moduleContent()	{
					
					switch((string)$this->MOD_SETTINGS['function'])	{
						case 1:


							if(isset($_POST['mt_add_allposts']))
							{
							 	$content.=$this->add_posts();
							}else
							if($_POST['allurl']=='' && !isset($_POST['mt_add_allposts']))
							{
								$text='';
								
								if(isset($_POST['all_links'])) $text=implode('',explode(';',$_POST['all_links']));
								$content.='
									<label>Import url</label>
									<textarea name="allurl" cols="100" rows="5">'.$text.'</textarea>
									<input name="" type="submit" />
								
								';
							}elseif(!isset($_POST['mt_add_allposts']))
							{
								if($_POST['all_post']=='' && !isset($_POST['mt_next']))
								{
									
									$allurl=$_POST['allurl'];
									$allurl=explode("\n",$allurl);
									if(isset($_POST['all_links'])) $all_links=explode(';',$_POST['all_links']);
									else $all_links=$allurl;
									//print_r($allurl);
									$replace=array("\n",'  ');
									$tothis=array('','');
									$remove_url=array('wp-content','category','wp-login.php','/tag/','xmlrpc.php','/feed/','page/2/','page/3','page/4','page/5','/rejestracja/','/o-mnie/','/jak-dodac-artykul/','/kontakt/');
									
									for($i=0; $i<1; $i++)
									{
										$is_url=$allurl[$i];
										$is_url=str_replace(chr(13),'',$is_url);
										$page_is_url=$is_url;
										if(stristr($is_url,'/page/'))
										{
											list($page_is_url)=explode('/page/',$is_url);
										}
										
										$add= $this->get($is_url);
										
										if(stristr($add,'post') || 1==1)
										{
											
											preg_match_all('/href=[\"\']?([^\"\'>]*)[\"\']?[^>]*>(.*?)<\/a>/si',$add,$out,PREG_SET_ORDER);
											
											for($z=0; $z<count($out); $z++)
											{
											
												$www_url=$out[$z][1];
												if($this->url_remove($remove_url,$www_url)==true && !stristr($www_url,$remove_url) && stristr($www_url,$is_url) || $this->url_remove($remove_url,$www_url)==true && !stristr($www_url,$remove_url) && stristr($www_url,$page_is_url))
												{
													
													$is_text=0;
													if($is_url!=$page_is_url) $check_www=str_replace($page_is_url,'',$www_url);
													else $check_www=str_replace($is_url,'',$www_url);
													$check_www=explode('/',$check_www);
													//print_r($check_www);
													for($x=0; $x<count($check_www); $x++)
													{
														if(intval($check_www[$x])==0 && $check_www[$x]!='') $is_text=1;
													}
													
													if($is_text==1) 
													{
														if(stristr($out[$z][1],'#')) list($out[$z][1])=explode('#',$out[$z][1]);
														if(!in_array($out[$z][1],$www_isadd)) 
														{
															$content.='<input name="all_posts[]" type="checkbox" value="'.$out[$z][1].'" /> - '.$out[$z][1]."<br />";
															$www_isadd[]=$out[$z][1];
														}
													}
													
												}
											}										
										}
									}
									unset($all_links[0]);
									$all_links=array_values($all_links);
									$content.='
										<input name="" type="submit" /> or <input name="mt_next" type="submit" value="Next" />
										<input name="all_post" type="hidden" value="1" />
										<input name="allurl" type="hidden" value="'.$is_url.'" />		
										<input name="all_links" type="hidden" value="'.implode(';',$all_links).'" />									
									';
								}
								elseif(!isset($_POST['mt_next']))
								{ 
									$allurl=$_POST['all_posts'];
									$is_url=$_POST['allurl'];
									
									$replace=array("\n",'  ');
									$tothis=array('','');
									$text_count=1;
									
									$add_data_content_header=array();
									
									for($i=0; $i<count($allurl); $i++)
									{
										$www=$allurl[$i];
										if(stristr($www,'#'))
										{
											list($www)=explode('#',$www);
											list($all_content,$data_content_header,$data_content_body)=$this->addtext($www,$i);
										}else
										{
											list($all_content)=$this->addtext($www,$i);
										}	

										$add_all_content[]=$all_content;			

										sleep(5);											
									}
										
									
									$all_content=$add_all_content;
									$dont_use_tag=array();
																		
									for($i=0; $i<count($all_content); $i++)
									{
										$header=$body=$tag=$category=array();
										
										$all_old_tags=$dont_use_tag;
										$dont_use_tag=array();
										
										$all_tags=$all_content[$i][3];										
										for($z=0; $z<count($all_tags); $z++)
										{
											$dont_use_tag[]=$all_tags[$z];
										}									
										for($z=0; $z<count($all_content[$i][0]); $z++)
										{
											if(!in_array($all_content[$i][0][$z],$add_header))
											{
												$header[]='<input type="checkbox" name="mt_header['.$i.'][]" value="1" checked="checked" /> <input name="mt_header_text['.$i.'][]" type="text" size="80" value="'.str_replace('  ','',$all_content[$i][0][$z]).'" /><br />';
												$add_header[]=$all_content[$i][0][$z];
											}
										}
										for($z=0; $z<count($all_content[$i][1]); $z++)
										{
											if(!in_array($all_content[$i][1][$z],$add_body) && $all_content[$i][1][$z]!='')
											{
												$body[]='<input type="checkbox" name="mt_body['.$i.'][]" value="1" checked="checked" /> <textarea name="mt_body_text['.$i.'][]" cols="100" rows="7">'.$all_content[$i][1][$z].'</textarea><br /><br />';
												$add_body[]=$all_content[$i][1][$z];
											}
										}
										for($z=0; $z<count($all_content[$i][2]); $z++)
										{
											$category[]='<input type="checkbox" name="mt_category['.$i.'][]" value="'.$all_content[$i][2][$z].'" checked="checked" /> '.$all_content[$i][2][$z].'<br />';
										}
										
										for($z=0; $z<count($all_content[$i][3]); $z++)
										{

											if(!in_array($all_content[$i][3][$z],$all_old_tags)) $tag[]='<input type="checkbox" name="mt_tag['.$i.'][]" value="'.$all_content[$i][3][$z].'" checked="checked" /> '.$all_content[$i][3][$z].'<br />';
										}
										if($i!=0)
										{
											$content.='
												<div style="border:1px solid;" class="mtis_rot">
													<b>
													<input type="checkbox" name="mt_addtext'.$i.'" value="1" checked="checked" /> Dodaj text
													</b>
													<fieldset>
														<legend>Nagłówek</legend>
														'.implode("\n",$header).'
													</fieldset>
													<fieldset>
														<legend>Kategorie</legend>
														'.implode("\n",$category).'
													</fieldset>
													<fieldset>
														<legend>Tagi</legend>
														'.implode("\n",$tag).'
													</fieldset>
													'.implode("\n",$body).'
												</div>
												'."\n";
											$text_count++;
										}
									
									}	
									$content.='
									<input name="mt_count_post" type="hidden" value="'.$text_count.'" />
									<input name="allurl" type="hidden" value="'.$is_url.'" />	
									<input name="mt_add_allposts" type="submit" />
									<input name="all_links" type="hidden" value="'.$_POST['all_links'].'" />		
									';						
								}
								else
								{
								$all_links=explode(';',$_POST['all_links']);
								$all_links=implode('',$all_links);
								$content.='
									<label>Import url</label>
									<textarea name="allurl" cols="100" rows="5">'.$all_links.'</textarea>
									<input name="" type="submit" />
								
								';
								}
								
							}
							$this->content.=$this->doc->section('Message #1:',$content,0,1);
						break;
						case 2:
							$content=$this->gen_allpost();
							$this->content.=$this->doc->section('Message #2:',$content,0,1);
						break;
						case 3:
							$content='<div align=center><strong>Menu item #3...</strong></div>';
							$this->content.=$this->doc->section('Message #3:',$content,0,1);
						break;
					}
				}
				function gen_allpost()
				{
					
				}
				function addtext($www,$number)
				{
					$add= $this->get($www);
					
					preg_match_all('#<p[^>]*>(.*)</p>#isU',$add,$return,PREG_SET_ORDER);
					for($z=0; $z<count($return); $z++)
					{
						$it_all=strip_tags(str_replace($replace,$tothis,$return[$z][1]));
						$is_all[]=$it_all;
						$alllen[]=strlen($it_all);
					}
					preg_match_all('#<h1[^>]*>(.*)</h1>#isU',$add,$h1,PREG_SET_ORDER);
					preg_match_all('#<h2[^>]*>(.*)</h2>#isU',$add,$h2,PREG_SET_ORDER);
					preg_match_all('#<h3[^>]*>(.*)</h3>#isU',$add,$h3,PREG_SET_ORDER);
					
					preg_match_all('/href=[\"\']?([^\"\'>]*)[\"\']?[^>]*>(.*?)<\/a>/si',$add,$link,PREG_SET_ORDER);
					for($i=0; $i<count($link); $i++)
					{
						if(stristr($link[$i][0],'rel="category')) $add_category[]=$link[$i][2];
						if(stristr($link[$i][0],'rel="tag')) $add_tag[]=$link[$i][2];
					}
					
						$header=$body=array();
						for($z=0; $z<count($h1); $z++)
						{
							$data_content_header[]=strip_tags($h1[$z][1]);
						}
						for($z=0; $z<count($h2); $z++)
						{
							$data_content_header[]=strip_tags($h2[$z][1]);
						}
						for($z=0; $z<count($h3); $z++)
						{
							$data_content_header[]=strip_tags($h3[$z][1]);
						}
						
						for($z=0; $z<count($is_all); $z++)
						{
							$data_content_body[]=strip_tags($is_all[$z]);
						}
						$all_content=array($data_content_header,$data_content_body,$add_category,$add_tag);
					
					return array($all_content,$data_content_header,$data_content_body);
					
				}
				function get($url) {
					$process = curl_init($url);
					curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
					curl_setopt($process, CURLOPT_HEADER, 0);
					curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
					if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
					if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
					curl_setopt($process,CURLOPT_ENCODING , $this->compression);
					curl_setopt($process, CURLOPT_TIMEOUT, 30000);
					if ($this->proxy) curl_setopt($process, CURLOPT_PROXY, $this->proxy);
					curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
					curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
					$return = curl_exec($process);
					curl_close($process);
					return $return; 
				} 
				
		}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/mt_importernews/mod1/index.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/mt_importernews/mod1/index.php']);
}




// Make instance:
$SOBE = t3lib_div::makeInstance('tx_mtimporternews_module1');
$SOBE->init();

// Include files?
foreach($SOBE->include_once as $INC_FILE)	include_once($INC_FILE);

$SOBE->main();
$SOBE->printContent();

?>